#include "stdafx.h"
#include "ReservedBufferWriter.h"
#include "IPALDebuggingServices.h"



ReservedBufferWriter::ReservedBufferWriter()
{

	NOTIMPL

}

ReservedBufferWriter::~ReservedBufferWriter()
{

	NOTIMPL

}

void ReservedBufferWriter::Initialize()
{

	this->m_pFirstBlock = 0;
	this->m_firstBlockSize = 0;
	this->m_pSecondBlock = 0;
	this->m_secondBlockSize = 0;
	this->m_offset = 0;

}

void ReservedBufferWriter::SetType(XUINT32 type)
{

	XASSERT(m_offset == m_firstBlockSize + m_secondBlockSize);


	memcpy(this->m_pFirstBlock, &type, MIN(this->m_firstBlockSize, 4u));
	if ( this->m_firstBlockSize < 4 )
		memcpy(this->m_pSecondBlock, (char *)&type + this->m_firstBlockSize, 4 - this->m_firstBlockSize);

}

void ReservedBufferWriter::WriteBytes(XUINT32 cSize, void * pSource)
{

	unsigned int sizeToWriteInFirstBlock;

	XASSERT(m_offset + cSize <= m_firstBlockSize + m_secondBlockSize);
	if ( cSize )
	{
		sizeToWriteInFirstBlock = MIN(cSize, this->m_firstBlockSize - this->m_offset);
		if ( this->m_offset < this->m_firstBlockSize )
		{
			memcpy((char *)this->m_pFirstBlock + this->m_offset, pSource, sizeToWriteInFirstBlock);
			this->m_offset += sizeToWriteInFirstBlock;
			pSource = (char *)pSource + sizeToWriteInFirstBlock;
			cSize -= sizeToWriteInFirstBlock;
		}
		if ( cSize )
		{
			memcpy((char *)this->m_pSecondBlock + this->m_offset - this->m_firstBlockSize, pSource, cSize);
			this->m_offset += cSize;
		}
	}

}
